package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.missilike.biz.IMilAdvertService;
import com.missilike.biz.IMilPhotoService;
import com.missilike.core.service.IOSSServcice;
import com.missilike.core.utils.SecurityUtils;
import com.missilike.domain.request.MilAdvertForm;
import com.missilike.domain.response.MilAdvertInfo;
import com.missilike.repository.entity.MilAdvert;
import com.missilike.repository.entity.MilPhoto;
import com.missilike.repository.mapper.MilAdvertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 广告 服务实现类
 * </p>
 *
 * @author kin
 * @since 2018-12-05
 */
@Service
public class MilAdvertServiceImpl extends ServiceImpl<MilAdvertMapper, MilAdvert> implements IMilAdvertService {

    private final IMilPhotoService milPhotoService;

    private final IOSSServcice aliYunOSSService;

    @Autowired
    public MilAdvertServiceImpl(@Qualifier("OSSService") IOSSServcice aliYunOSSService, IMilPhotoService milPhotoService) {
        this.aliYunOSSService = aliYunOSSService;
        this.milPhotoService = milPhotoService;
    }

    @Transactional
    @Override
    public void upsert(MilAdvertForm milAdvertForm) {
        if (milAdvertForm.getFile() != null) {
            //上传图片到OSS
            String filePath = aliYunOSSService.updateImages(milAdvertForm.getFile());
            milAdvertForm.setImageUrlId(milPhotoService.upsertPhotoId(milAdvertForm.getImageUrlId(), filePath));
        }
        if (milAdvertForm.getId() == null) {
            milAdvertForm.setCreateUserId(SecurityUtils.getCurrentUserId());
        } else {
            milAdvertForm.setUpdateUserId(SecurityUtils.getCurrentUserId());
        }
        this.saveOrUpdate(milAdvertForm);
    }

    @Override
    public List<MilAdvertInfo> getList(int pageNum, int pageSize) {
        List<MilAdvertInfo> milAdvertInfos = new ArrayList<>();
        Page<MilAdvert> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MilAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .orderByDesc("create_time");
        IPage<MilAdvert> milAdvertIPage = this.page(page, queryWrapper);
        if (milAdvertIPage.getRecords() != null && milAdvertIPage.getRecords().size() > 0) {
            milAdvertIPage.getRecords().forEach(milAdvert->{
                MilAdvertInfo milAdvertInfo = greateMilAdvertInfo(milAdvert);
                milAdvertInfos.add(milAdvertInfo);
            });
        }
        return milAdvertInfos;
    }

    private MilAdvertInfo greateMilAdvertInfo(MilAdvert milAdvert) {
        MilAdvertInfo milAdvertInfo = new MilAdvertInfo();
        milAdvertInfo.setCreateTime(milAdvert.getCreateTime());
        milAdvertInfo.setCreateUserId(milAdvert.getCreateUserId());
        milAdvertInfo.setId(milAdvert.getId());
        milAdvertInfo.setImageUrl(milPhotoService.getPhotoUrl(milAdvert.getImageUrlId()));
        milAdvertInfo.setPosition(milAdvert.getPosition());
        milAdvertInfo.setRemark(milAdvert.getRemark());
        milAdvertInfo.setStatus(milAdvert.getStatus());
        milAdvertInfo.setTitle(milAdvert.getTitle());
        milAdvertInfo.setType(milAdvert.getType());
        milAdvertInfo.setUrl(milAdvert.getUrl());
        return milAdvertInfo;
    }

    @Override
    public IPage<MilAdvert> getList(Page<MilAdvert> page) {
        return this.page(page, null);
    }

    @Override
    public MilAdvertInfo getNew(){
        QueryWrapper<MilAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .orderByDesc("create_time");
        MilAdvert milAdvert = this.getOne(queryWrapper);

        return greateMilAdvertInfo(milAdvert);
    }
}
